package com.gugee.tiktok.web.repository.impl;

import com.gugee.tiktok.web.dto.RankSearchData;
import com.gugee.tiktok.web.dto.RankingFollowerResponse;
import com.gugee.tiktok.web.model.mysql.RankingFollowers;
import com.gugee.tiktok.web.repository.RankingFollowersExtRepository;
import com.xiguaji.boot.toolkit.jdbc.JdbcTemplateHelper;
import com.xiguaji.boot.toolkit.jdbc.Page;
import lombok.val;

import java.util.ArrayList;
import java.util.List;

public class RankingFollowersExtRepositoryImpl implements RankingFollowersExtRepository {


    @Override
    public Page<RankingFollowerResponse> getListForPage(RankSearchData searchData){
        StringBuffer sql = new StringBuffer("FROM ranking_followers r left join blogger_main b on r.uid = b.o_uid ");
        List<Object> params = new ArrayList<>();
        sql.append("WHERE r.ranking_type = ? ");
        params.add(searchData.getRankingType());
        sql.append("AND r.belong = ? ");
        params.add(searchData.getDate());
        sql.append("AND r.blogger_type = ? ");

        if(null == searchData.getBloggerTypeId()){
            searchData.setBloggerTypeId(0);

        }
        params.add(searchData.getBloggerTypeId());
        sql.append("ORDER BY r.ranking ");
        val page = JdbcTemplateHelper.paginate(RankingFollowerResponse.class, searchData.getPage(), searchData.getPageSize(), "SELECT r.*,REPLACE(REPLACE (REPLACE (r.avatar, 'sign-sg', 'amd-va'), 'sign-va', 'amd-va'),'webp','jpeg') avatar,b.score ", sql.toString(), params.toArray());
        return page;
    }

    @Override
    public List<RankingFollowers> getList(RankSearchData searchData, Integer count) {
        StringBuffer sql = new StringBuffer("SELECT * FROM ranking_followers r ");
        List<Object> params = new ArrayList<>();
        sql.append("WHERE r.ranking_type = ? ");
        params.add(searchData.getRankingType());
        sql.append("AND r.belong = ? ");
        params.add(searchData.getDate());
        sql.append("AND r.blogger_type = ? ");

        if (null == searchData.getBloggerTypeId()) {
            searchData.setBloggerTypeId(0);

        }
        params.add(searchData.getBloggerTypeId());
        sql.append("ORDER BY r.ranking ");

        if(null != count){
            sql.append("limit 0,?");
            params.add(count);
        }
        return JdbcTemplateHelper.findEntity(RankingFollowers.class, sql.toString(), params.toArray());
    }
}
